using B.S.Data.MES.API.Applications.BaseData.Command.MaterialGroupCommand;
using B.S.Domain.BasicData.MaterialManagement.MaterialGroups;
using B.S.Infratrctrue;
using B.S.Requst;
using MediatR;

namespace B.S.Data.MES.API.Applications.BaseData.CommandHandlers.MaterialGroupCommandHandlers
{
    public class DeleteMaterialGroupCommandHandler : IRequestHandler<DeleteMaterialGroupCommand, APIRequst<int>>
    {
        private readonly IBaseRepository<Material_group> _materialGroupRepository;

        public DeleteMaterialGroupCommandHandler(IBaseRepository<Material_group> materialGroupRepository)
        {
            _materialGroupRepository = materialGroupRepository;
        }

        public async Task<APIRequst<int>> Handle(DeleteMaterialGroupCommand request, CancellationToken cancellationToken)
        {
            var existingMaterialGroup = _materialGroupRepository.GetAll().FirstOrDefault(x => x.MaterId == request.MaterId);
            existingMaterialGroup.isDel = true; 
            await _materialGroupRepository.Update(existingMaterialGroup);
            return new APIRequst<int>
            {
                Code = RequstCode.成功,
                Msg=RequstCode.成功.ToString()
            };
        }
    }
}